﻿using liaowa2.Utils;
using liaowa2.Utils.Cookie;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace liaowa2.Web.App_Start
{
    public class MyActionFilter : ActionFilterAttribute
    {
        private static readonly string HttpMehotdPost = "POST";

        //方法执行之前，进行权限判断
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controllerContext = filterContext.Controller.ControllerContext;
            var req = controllerContext.RequestContext.HttpContext.Request;
            if (req.HttpMethod == HttpMehotdPost && req.UrlReferrer == null)
            {
                return;
            }

            base.OnActionExecuting(filterContext);

            //#region 后台以及个人端、企业端没有登录的，跳转到登录页面。

            //var area = string.Empty;
            //if (filterContext.RouteData.DataTokens["area"] != null)
            //{
            //    area = filterContext.RouteData.DataTokens["area"].ToString().ToLower();
            //}

            //string controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
            //string action = filterContext.RouteData.Values["action"].ToString().ToLower();
            //if (!IsAjax(filterContext) && string.IsNullOrEmpty(CookieHelper.GetCookieValue(ParameterConfig.CookieName))
            //                    && controller.ToLower() != "login" && controller.ToLower() != "finduserpwd")
            //{
            //    filterContext.Result = new RedirectResult("/Login/Index");
            //}

            //#endregion
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
        }

        #region 自定义

        private bool IsAjax(ActionExecutingContext filterContext)
        {

            return filterContext.HttpContext.Request.IsAjaxRequest();
        }


        #endregion

    }
}